package org.shop.controller.manager;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.shop.entity.Goods;
import org.shop.entity.SystemMessage;
import org.shop.entity.Users;
import org.shop.entity.security.MyUserDetail;
import org.shop.service.GoodsService;
import org.shop.service.SystemMessageService;
import org.shop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class ManagerGoodsController {

	private static Logger logger = LogManager.getLogger(ManagerGoodsController.class);
	
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private SystemMessageService systemMessageService;
    @Autowired
    private UserService userService;
	

    /**
     * 获取审核的商品列表
     * @param req
     * @return
     */
    @RequestMapping("manager/getCheckGoodsList")
    @ResponseBody
    public Map<String, Object> getCheckGoodsList(HttpServletRequest req){
        Map<String, Object> map = new HashMap<String, Object>();
        String currentPage = req.getParameter("offset");
        String pageSize = req.getParameter("limit");
        String goodsName = req.getParameter("goodsName");
        String userName = req.getParameter("userName");
        String oldMerchant = req.getParameter("oldMerchant");
        String merchantId = req.getParameter("merchantId");
        try {
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("startPage", Integer.parseInt(currentPage));
            paramMap.put("pageSize", Integer.parseInt(pageSize));
            paramMap.put("goodsName", goodsName);
            paramMap.put("userName", userName);
            paramMap.put("oldMerchant", oldMerchant);
            paramMap.put("merchantId", merchantId);
            map = goodsService.getCheckGoodsList(paramMap);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e);
        }
        return map;
    }
    
    /**
     * 通过商品ID获取商品信息
     * @param req
     * @return
     */
    @RequestMapping("manager/getGoodsInfo")
    @ResponseBody
    public Goods getManagerGoodsInfo(HttpServletRequest req){
        String goodsId = req.getParameter("goodsId");
        Goods goodsInfo = null;
        try {
            goodsInfo = goodsService.selectByPrimaryKey(Long.parseLong(goodsId));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e);
        }
        return goodsInfo;
    }
    
    /**
     * 商品关闭
     * @param req
     * @return
     */
    @RequestMapping("manager/closeGoods")
    @ResponseBody
    public Map<String, Object> closeGoods(HttpServletRequest req){
        Map<String, Object> map = new HashMap<String, Object>();
        String goodsId = req.getParameter("goodsId");
        //获取登录人用户信息
      	MyUserDetail ud = (MyUserDetail) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        try {
        	Goods selectByPrimaryKey = goodsService.selectByPrimaryKey(Long.parseLong(goodsId));
        	Users userInfoByConditions = userService.selectByPrimaryKey(selectByPrimaryKey.getUserId());
        	SystemMessage systemMessage = new SystemMessage();
        	systemMessage.setreceiveId(userInfoByConditions.getUserId());
        	systemMessage.setSendId(ud.getUserId());
        	systemMessage.setMessageContent("您的商品名叫："+selectByPrimaryKey.getGoodsName()+"由于涉嫌某种原因不合法,已被关闭,请遵守商城规则.若需要反驳,请联系客服进行打开商品.");
			systemMessageService.insertSelective(systemMessage );
			selectByPrimaryKey.setState((byte)0);
            goodsService.updateByPrimaryKeySelective(selectByPrimaryKey);
            map.put("state", 1);
            map.put("msg", "关闭成功");
        } catch(Exception e) {
            map.put("state", 0);
            map.put("msg", "关闭失败");
            logger.error(e);
        }
        return map;
    }
    /**
     * 商品打开
     * @param req
     * @return
     */
    @RequestMapping("manager/openGoods")
    @ResponseBody
    public Map<String, Object> openGoods(HttpServletRequest req){
        Map<String, Object> map=new HashMap<String, Object>();
        String goodsId = req.getParameter("goodsId");
        //获取登录人用户信息
      	MyUserDetail ud = (MyUserDetail) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        try {
        	Goods selectByPrimaryKey = goodsService.selectByPrimaryKey(Long.parseLong(goodsId));
        	Users userInfoByConditions = userService.selectByPrimaryKey(selectByPrimaryKey.getUserId());
        	SystemMessage systemMessage = new SystemMessage();
        	systemMessage.setreceiveId(userInfoByConditions.getUserId());
        	systemMessage.setSendId(ud.getUserId());
        	systemMessage.setMessageContent("您的商品名叫："+selectByPrimaryKey.getGoodsName()+",已被打开,请遵守商城规则.");
			systemMessageService.insertSelective(systemMessage );
			selectByPrimaryKey.setState((byte)2);
            goodsService.updateByPrimaryKeySelective(selectByPrimaryKey);
            map.put("state", 1);
            map.put("msg", "打开成功");
        } catch(Exception e) {
            map.put("state", 0);
            map.put("msg", "打开失败");
            logger.error(e);
        }
        return map;
    }
    
}
